[llvm] 888228f - AMDGPU: Use early continue to reduce indentation

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 22 09:39:06 PST 2022


Author: Matt Arsenault
Date: 2022-12-22T12:38:59-05:00
New Revision: 888228f2b04580d9b0dddb1b0e3a07d7b8aa8312

URL: https://github.com/llvm/llvm-project/commit/888228f2b04580d9b0dddb1b0e3a07d7b8aa8312
DIFF: https://github.com/llvm/llvm-project/commit/888228f2b04580d9b0dddb1b0e3a07d7b8aa8312.diff

LOG: AMDGPU: Use early continue to reduce indentation

Added: 
    

Modified: 
    llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp b/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp
index 61e48e29aeeb..92fd7bc87716 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp
@@ -177,361 +177,360 @@ bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
     }
 
     ConstantExpr *ConstExpr = dyn_cast<ConstantExpr>(Op);
+    if (!ConstExpr) {
+      continue;
+    }
 
-    if (ConstExpr) {
-      GlobalVariable *GVar = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
-
-      StringRef Str("unknown");
-      if (GVar && GVar->hasInitializer()) {
-        auto *Init = GVar->getInitializer();
-        if (auto *CA = dyn_cast<ConstantDataArray>(Init)) {
-          if (CA->isString())
-            Str = CA->getAsCString();
-        } else if (isa<ConstantAggregateZero>(Init)) {
-          Str = "";
-        }
-        //
-        // we need this call to ascertain
-        // that we are printing a string
-        // or a pointer. It takes out the
-        // specifiers and fills up the first
-        // arg
-        getConversionSpecifiers(OpConvSpecifiers, Str, NumOps - 1);
+    GlobalVariable *GVar = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
+
+    StringRef Str("unknown");
+    if (GVar && GVar->hasInitializer()) {
+      auto *Init = GVar->getInitializer();
+      if (auto *CA = dyn_cast<ConstantDataArray>(Init)) {
+        if (CA->isString())
+          Str = CA->getAsCString();
+      } else if (isa<ConstantAggregateZero>(Init)) {
+        Str = "";
       }
-      // Add metadata for the string
-      std::string AStreamHolder;
-      raw_string_ostream Sizes(AStreamHolder);
-      int Sum = DWORD_ALIGN;
-      Sizes << CI->arg_size() - 1;
-      Sizes << ':';
-      for (unsigned ArgCount = 1;
-           ArgCount < CI->arg_size() && ArgCount <= OpConvSpecifiers.size();
-           ArgCount++) {
-        Value *Arg = CI->getArgOperand(ArgCount);
-        Type *ArgType = Arg->getType();
-        unsigned ArgSize = TD->getTypeAllocSizeInBits(ArgType);
+      //
+      // we need this call to ascertain
+      // that we are printing a string
+      // or a pointer. It takes out the
+      // specifiers and fills up the first
+      // arg
+      getConversionSpecifiers(OpConvSpecifiers, Str, NumOps - 1);
+    }
+    // Add metadata for the string
+    std::string AStreamHolder;
+    raw_string_ostream Sizes(AStreamHolder);
+    int Sum = DWORD_ALIGN;
+    Sizes << CI->arg_size() - 1;
+    Sizes << ':';
+    for (unsigned ArgCount = 1;
+         ArgCount < CI->arg_size() && ArgCount <= OpConvSpecifiers.size();
+         ArgCount++) {
+      Value *Arg = CI->getArgOperand(ArgCount);
+      Type *ArgType = Arg->getType();
+      unsigned ArgSize = TD->getTypeAllocSizeInBits(ArgType);
+      ArgSize = ArgSize / 8;
+      //
+      // ArgSize by design should be a multiple of DWORD_ALIGN,
+      // expand the arguments that do not follow this rule.
+      //
+      if (ArgSize % DWORD_ALIGN != 0) {
+        llvm::Type *ResType = llvm::Type::getInt32Ty(Ctx);
+        auto *LLVMVecType = llvm::dyn_cast<llvm::FixedVectorType>(ArgType);
+        int NumElem = LLVMVecType ? LLVMVecType->getNumElements() : 1;
+        if (LLVMVecType && NumElem > 1)
+          ResType = llvm::FixedVectorType::get(ResType, NumElem);
+        Builder.SetInsertPoint(CI);
+        Builder.SetCurrentDebugLocation(CI->getDebugLoc());
+        if (OpConvSpecifiers[ArgCount - 1] == 'x' ||
+            OpConvSpecifiers[ArgCount - 1] == 'X' ||
+            OpConvSpecifiers[ArgCount - 1] == 'u' ||
+            OpConvSpecifiers[ArgCount - 1] == 'o')
+          Arg = Builder.CreateZExt(Arg, ResType);
+        else
+          Arg = Builder.CreateSExt(Arg, ResType);
+        ArgType = Arg->getType();
+        ArgSize = TD->getTypeAllocSizeInBits(ArgType);
         ArgSize = ArgSize / 8;
-        //
-        // ArgSize by design should be a multiple of DWORD_ALIGN,
-        // expand the arguments that do not follow this rule.
-        //
-        if (ArgSize % DWORD_ALIGN != 0) {
-          llvm::Type *ResType = llvm::Type::getInt32Ty(Ctx);
-          auto *LLVMVecType = llvm::dyn_cast<llvm::FixedVectorType>(ArgType);
-          int NumElem = LLVMVecType ? LLVMVecType->getNumElements() : 1;
-          if (LLVMVecType && NumElem > 1)
-            ResType = llvm::FixedVectorType::get(ResType, NumElem);
-          Builder.SetInsertPoint(CI);
-          Builder.SetCurrentDebugLocation(CI->getDebugLoc());
-          if (OpConvSpecifiers[ArgCount - 1] == 'x' ||
-              OpConvSpecifiers[ArgCount - 1] == 'X' ||
-              OpConvSpecifiers[ArgCount - 1] == 'u' ||
-              OpConvSpecifiers[ArgCount - 1] == 'o')
-            Arg = Builder.CreateZExt(Arg, ResType);
-          else
-            Arg = Builder.CreateSExt(Arg, ResType);
-          ArgType = Arg->getType();
-          ArgSize = TD->getTypeAllocSizeInBits(ArgType);
-          ArgSize = ArgSize / 8;
-          CI->setOperand(ArgCount, Arg);
-        }
-        if (OpConvSpecifiers[ArgCount - 1] == 'f') {
-          ConstantFP *FpCons = dyn_cast<ConstantFP>(Arg);
-          if (FpCons)
+        CI->setOperand(ArgCount, Arg);
+      }
+      if (OpConvSpecifiers[ArgCount - 1] == 'f') {
+        ConstantFP *FpCons = dyn_cast<ConstantFP>(Arg);
+        if (FpCons)
+          ArgSize = 4;
+        else {
+          FPExtInst *FpExt = dyn_cast<FPExtInst>(Arg);
+          if (FpExt && FpExt->getType()->isDoubleTy() &&
+              FpExt->getOperand(0)->getType()->isFloatTy())
             ArgSize = 4;
-          else {
-            FPExtInst *FpExt = dyn_cast<FPExtInst>(Arg);
-            if (FpExt && FpExt->getType()->isDoubleTy() &&
-                FpExt->getOperand(0)->getType()->isFloatTy())
-              ArgSize = 4;
-          }
         }
-        if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) {
-          if (auto *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
-            auto *GV = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
-            if (GV && GV->hasInitializer()) {
-              Constant *Init = GV->getInitializer();
-              bool IsZeroValue = Init->isZeroValue();
-              auto *CA = dyn_cast<ConstantDataArray>(Init);
-              if (IsZeroValue || (CA && CA->isString())) {
-                size_t SizeStr =
-                    IsZeroValue ? 1 : (strlen(CA->getAsCString().data()) + 1);
-                size_t Rem = SizeStr % DWORD_ALIGN;
-                size_t NSizeStr = 0;
-                LLVM_DEBUG(dbgs() << "Printf string original size = " << SizeStr
-                                  << '\n');
-                if (Rem) {
-                  NSizeStr = SizeStr + (DWORD_ALIGN - Rem);
-                } else {
-                  NSizeStr = SizeStr;
-                }
-                ArgSize = NSizeStr;
+      }
+      if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) {
+        if (auto *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
+          auto *GV = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
+          if (GV && GV->hasInitializer()) {
+            Constant *Init = GV->getInitializer();
+            bool IsZeroValue = Init->isZeroValue();
+            auto *CA = dyn_cast<ConstantDataArray>(Init);
+            if (IsZeroValue || (CA && CA->isString())) {
+              size_t SizeStr =
+                  IsZeroValue ? 1 : (strlen(CA->getAsCString().data()) + 1);
+              size_t Rem = SizeStr % DWORD_ALIGN;
+              size_t NSizeStr = 0;
+              LLVM_DEBUG(dbgs() << "Printf string original size = " << SizeStr
+                                << '\n');
+              if (Rem) {
+                NSizeStr = SizeStr + (DWORD_ALIGN - Rem);
+              } else {
+                NSizeStr = SizeStr;
               }
-            } else {
-              ArgSize = sizeof(NonLiteralStr);
+              ArgSize = NSizeStr;
             }
           } else {
             ArgSize = sizeof(NonLiteralStr);
           }
-        }
-        LLVM_DEBUG(dbgs() << "Printf ArgSize (in buffer) = " << ArgSize
-                          << " for type: " << *ArgType << '\n');
-        Sizes << ArgSize << ':';
-        Sum += ArgSize;
-      }
-      LLVM_DEBUG(dbgs() << "Printf format string in source = " << Str.str()
-                        << '\n');
-      for (char C : Str) {
-        // Rest of the C escape sequences (e.g. \') are handled correctly
-        // by the MDParser
-        switch (C) {
-        case '\a':
-          Sizes << "\\a";
-          break;
-        case '\b':
-          Sizes << "\\b";
-          break;
-        case '\f':
-          Sizes << "\\f";
-          break;
-        case '\n':
-          Sizes << "\\n";
-          break;
-        case '\r':
-          Sizes << "\\r";
-          break;
-        case '\v':
-          Sizes << "\\v";
-          break;
-        case ':':
-          // ':' cannot be scanned by Flex, as it is defined as a delimiter
-          // Replace it with it's octal representation \72
-          Sizes << "\\72";
-          break;
-        default:
-          Sizes << C;
-          break;
+        } else {
+          ArgSize = sizeof(NonLiteralStr);
         }
       }
-
-      // Insert the printf_alloc call
-      Builder.SetInsertPoint(CI);
-      Builder.SetCurrentDebugLocation(CI->getDebugLoc());
-
-      AttributeList Attr = AttributeList::get(Ctx, AttributeList::FunctionIndex,
-                                              Attribute::NoUnwind);
-
-      Type *SizetTy = Type::getInt32Ty(Ctx);
-
-      Type *Tys_alloc[1] = {SizetTy};
-      Type *I8Ty = Type::getInt8Ty(Ctx);
-      Type *I8Ptr = PointerType::get(I8Ty, 1);
-      FunctionType *FTy_alloc = FunctionType::get(I8Ptr, Tys_alloc, false);
-      FunctionCallee PrintfAllocFn =
-          M.getOrInsertFunction(StringRef("__printf_alloc"), FTy_alloc, Attr);
-
-      LLVM_DEBUG(dbgs() << "Printf metadata = " << Sizes.str() << '\n');
-      std::string fmtstr = itostr(++UniqID) + ":" + Sizes.str();
-      MDString *fmtStrArray = MDString::get(Ctx, fmtstr);
-
-      // Instead of creating global variables, the
-      // printf format strings are extracted
-      // and passed as metadata. This avoids
-      // polluting llvm's symbol tables in this module.
-      // Metadata is going to be extracted
-      // by the backend passes and inserted
-      // into the OpenCL binary as appropriate.
-      StringRef amd("llvm.printf.fmts");
-      NamedMDNode *metaD = M.getOrInsertNamedMetadata(amd);
-      MDNode *myMD = MDNode::get(Ctx, fmtStrArray);
-      metaD->addOperand(myMD);
-      Value *sumC = ConstantInt::get(SizetTy, Sum, false);
-      SmallVector<Value *, 1> alloc_args;
-      alloc_args.push_back(sumC);
-      CallInst *pcall =
-          CallInst::Create(PrintfAllocFn, alloc_args, "printf_alloc_fn", CI);
-
-      //
-      // Insert code to split basicblock with a
-      // piece of hammock code.
-      // basicblock splits after buffer overflow check
-      //
-      ConstantPointerNull *zeroIntPtr =
-          ConstantPointerNull::get(PointerType::get(I8Ty, 1));
-      auto *cmp = cast<ICmpInst>(Builder.CreateICmpNE(pcall, zeroIntPtr, ""));
-      if (!CI->use_empty()) {
-        Value *result =
-            Builder.CreateSExt(Builder.CreateNot(cmp), I32Ty, "printf_res");
-        CI->replaceAllUsesWith(result);
+      LLVM_DEBUG(dbgs() << "Printf ArgSize (in buffer) = " << ArgSize
+                        << " for type: " << *ArgType << '\n');
+      Sizes << ArgSize << ':';
+      Sum += ArgSize;
+    }
+    LLVM_DEBUG(dbgs() << "Printf format string in source = " << Str.str()
+                      << '\n');
+    for (char C : Str) {
+      // Rest of the C escape sequences (e.g. \') are handled correctly
+      // by the MDParser
+      switch (C) {
+      case '\a':
+        Sizes << "\\a";
+        break;
+      case '\b':
+        Sizes << "\\b";
+        break;
+      case '\f':
+        Sizes << "\\f";
+        break;
+      case '\n':
+        Sizes << "\\n";
+        break;
+      case '\r':
+        Sizes << "\\r";
+        break;
+      case '\v':
+        Sizes << "\\v";
+        break;
+      case ':':
+        // ':' cannot be scanned by Flex, as it is defined as a delimiter
+        // Replace it with it's octal representation \72
+        Sizes << "\\72";
+        break;
+      default:
+        Sizes << C;
+        break;
       }
-      SplitBlock(CI->getParent(), cmp);
-      Instruction *Brnch =
-          SplitBlockAndInsertIfThen(cmp, cmp->getNextNode(), false);
-
-      Builder.SetInsertPoint(Brnch);
+    }
 
-      // store unique printf id in the buffer
-      //
-      GetElementPtrInst *BufferIdx = GetElementPtrInst::Create(
-          I8Ty, pcall, ConstantInt::get(Ctx, APInt(32, 0)), "PrintBuffID",
-          Brnch);
-
-      Type *idPointer = PointerType::get(I32Ty, AMDGPUAS::GLOBAL_ADDRESS);
-      Value *id_gep_cast =
-          new BitCastInst(BufferIdx, idPointer, "PrintBuffIdCast", Brnch);
-
-      new StoreInst(ConstantInt::get(I32Ty, UniqID), id_gep_cast, Brnch);
-
-      // 1st 4 bytes hold the printf_id
-      // the following GEP is the buffer pointer
-      BufferIdx = GetElementPtrInst::Create(
-          I8Ty, pcall, ConstantInt::get(Ctx, APInt(32, 4)), "PrintBuffGep",
-          Brnch);
-
-      Type *Int32Ty = Type::getInt32Ty(Ctx);
-      Type *Int64Ty = Type::getInt64Ty(Ctx);
-      for (unsigned ArgCount = 1;
-           ArgCount < CI->arg_size() && ArgCount <= OpConvSpecifiers.size();
-           ArgCount++) {
-        Value *Arg = CI->getArgOperand(ArgCount);
-        Type *ArgType = Arg->getType();
-        SmallVector<Value *, 32> WhatToStore;
-        if (ArgType->isFPOrFPVectorTy() && !isa<VectorType>(ArgType)) {
-          Type *IType = (ArgType->isFloatTy()) ? Int32Ty : Int64Ty;
-          if (OpConvSpecifiers[ArgCount - 1] == 'f') {
-            if (auto *FpCons = dyn_cast<ConstantFP>(Arg)) {
-              APFloat Val(FpCons->getValueAPF());
-              bool Lost = false;
-              Val.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
-                          &Lost);
-              Arg = ConstantFP::get(Ctx, Val);
+    // Insert the printf_alloc call
+    Builder.SetInsertPoint(CI);
+    Builder.SetCurrentDebugLocation(CI->getDebugLoc());
+
+    AttributeList Attr = AttributeList::get(Ctx, AttributeList::FunctionIndex,
+                                            Attribute::NoUnwind);
+
+    Type *SizetTy = Type::getInt32Ty(Ctx);
+
+    Type *Tys_alloc[1] = {SizetTy};
+    Type *I8Ty = Type::getInt8Ty(Ctx);
+    Type *I8Ptr = PointerType::get(I8Ty, 1);
+    FunctionType *FTy_alloc = FunctionType::get(I8Ptr, Tys_alloc, false);
+    FunctionCallee PrintfAllocFn =
+        M.getOrInsertFunction(StringRef("__printf_alloc"), FTy_alloc, Attr);
+
+    LLVM_DEBUG(dbgs() << "Printf metadata = " << Sizes.str() << '\n');
+    std::string fmtstr = itostr(++UniqID) + ":" + Sizes.str();
+    MDString *fmtStrArray = MDString::get(Ctx, fmtstr);
+
+    // Instead of creating global variables, the
+    // printf format strings are extracted
+    // and passed as metadata. This avoids
+    // polluting llvm's symbol tables in this module.
+    // Metadata is going to be extracted
+    // by the backend passes and inserted
+    // into the OpenCL binary as appropriate.
+    StringRef amd("llvm.printf.fmts");
+    NamedMDNode *metaD = M.getOrInsertNamedMetadata(amd);
+    MDNode *myMD = MDNode::get(Ctx, fmtStrArray);
+    metaD->addOperand(myMD);
+    Value *sumC = ConstantInt::get(SizetTy, Sum, false);
+    SmallVector<Value *, 1> alloc_args;
+    alloc_args.push_back(sumC);
+    CallInst *pcall =
+        CallInst::Create(PrintfAllocFn, alloc_args, "printf_alloc_fn", CI);
+
+    //
+    // Insert code to split basicblock with a
+    // piece of hammock code.
+    // basicblock splits after buffer overflow check
+    //
+    ConstantPointerNull *zeroIntPtr =
+        ConstantPointerNull::get(PointerType::get(I8Ty, 1));
+    auto *cmp = cast<ICmpInst>(Builder.CreateICmpNE(pcall, zeroIntPtr, ""));
+    if (!CI->use_empty()) {
+      Value *result =
+          Builder.CreateSExt(Builder.CreateNot(cmp), I32Ty, "printf_res");
+      CI->replaceAllUsesWith(result);
+    }
+    SplitBlock(CI->getParent(), cmp);
+    Instruction *Brnch =
+        SplitBlockAndInsertIfThen(cmp, cmp->getNextNode(), false);
+
+    Builder.SetInsertPoint(Brnch);
+
+    // store unique printf id in the buffer
+    //
+    GetElementPtrInst *BufferIdx = GetElementPtrInst::Create(
+        I8Ty, pcall, ConstantInt::get(Ctx, APInt(32, 0)), "PrintBuffID", Brnch);
+
+    Type *idPointer = PointerType::get(I32Ty, AMDGPUAS::GLOBAL_ADDRESS);
+    Value *id_gep_cast =
+        new BitCastInst(BufferIdx, idPointer, "PrintBuffIdCast", Brnch);
+
+    new StoreInst(ConstantInt::get(I32Ty, UniqID), id_gep_cast, Brnch);
+
+    // 1st 4 bytes hold the printf_id
+    // the following GEP is the buffer pointer
+    BufferIdx = GetElementPtrInst::Create(I8Ty, pcall,
+                                          ConstantInt::get(Ctx, APInt(32, 4)),
+                                          "PrintBuffGep", Brnch);
+
+    Type *Int32Ty = Type::getInt32Ty(Ctx);
+    Type *Int64Ty = Type::getInt64Ty(Ctx);
+    for (unsigned ArgCount = 1;
+         ArgCount < CI->arg_size() && ArgCount <= OpConvSpecifiers.size();
+         ArgCount++) {
+      Value *Arg = CI->getArgOperand(ArgCount);
+      Type *ArgType = Arg->getType();
+      SmallVector<Value *, 32> WhatToStore;
+      if (ArgType->isFPOrFPVectorTy() && !isa<VectorType>(ArgType)) {
+        Type *IType = (ArgType->isFloatTy()) ? Int32Ty : Int64Ty;
+        if (OpConvSpecifiers[ArgCount - 1] == 'f') {
+          if (auto *FpCons = dyn_cast<ConstantFP>(Arg)) {
+            APFloat Val(FpCons->getValueAPF());
+            bool Lost = false;
+            Val.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
+                        &Lost);
+            Arg = ConstantFP::get(Ctx, Val);
+            IType = Int32Ty;
+          } else if (auto *FpExt = dyn_cast<FPExtInst>(Arg)) {
+            if (FpExt->getType()->isDoubleTy() &&
+                FpExt->getOperand(0)->getType()->isFloatTy()) {
+              Arg = FpExt->getOperand(0);
               IType = Int32Ty;
-            } else if (auto *FpExt = dyn_cast<FPExtInst>(Arg)) {
-              if (FpExt->getType()->isDoubleTy() &&
-                  FpExt->getOperand(0)->getType()->isFloatTy()) {
-                Arg = FpExt->getOperand(0);
-                IType = Int32Ty;
-              }
             }
           }
-          Arg = new BitCastInst(Arg, IType, "PrintArgFP", Brnch);
-          WhatToStore.push_back(Arg);
-        } else if (ArgType->getTypeID() == Type::PointerTyID) {
-          if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) {
-            const char *S = NonLiteralStr;
-            if (auto *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
-              auto *GV = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
-              if (GV && GV->hasInitializer()) {
-                Constant *Init = GV->getInitializer();
-                bool IsZeroValue = Init->isZeroValue();
-                auto *CA = dyn_cast<ConstantDataArray>(Init);
-                if (IsZeroValue || (CA && CA->isString())) {
-                  S = IsZeroValue ? "" : CA->getAsCString().data();
-                }
+        }
+        Arg = new BitCastInst(Arg, IType, "PrintArgFP", Brnch);
+        WhatToStore.push_back(Arg);
+      } else if (ArgType->getTypeID() == Type::PointerTyID) {
+        if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) {
+          const char *S = NonLiteralStr;
+          if (auto *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
+            auto *GV = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
+            if (GV && GV->hasInitializer()) {
+              Constant *Init = GV->getInitializer();
+              bool IsZeroValue = Init->isZeroValue();
+              auto *CA = dyn_cast<ConstantDataArray>(Init);
+              if (IsZeroValue || (CA && CA->isString())) {
+                S = IsZeroValue ? "" : CA->getAsCString().data();
               }
             }
-            size_t SizeStr = strlen(S) + 1;
-            size_t Rem = SizeStr % DWORD_ALIGN;
-            size_t NSizeStr = 0;
-            if (Rem) {
-              NSizeStr = SizeStr + (DWORD_ALIGN - Rem);
-            } else {
-              NSizeStr = SizeStr;
-            }
-            if (S[0]) {
-              char *MyNewStr = new char[NSizeStr]();
-              strcpy(MyNewStr, S);
-              int NumInts = NSizeStr / 4;
-              int CharC = 0;
-              while (NumInts) {
-                int ANum = *(int *)(MyNewStr + CharC);
-                CharC += 4;
-                NumInts--;
-                Value *ANumV = ConstantInt::get(Int32Ty, ANum, false);
-                WhatToStore.push_back(ANumV);
-              }
-              delete[] MyNewStr;
-            } else {
-              // Empty string, give a hint to RT it is no NULL
-              Value *ANumV = ConstantInt::get(Int32Ty, 0xFFFFFF00, false);
+          }
+          size_t SizeStr = strlen(S) + 1;
+          size_t Rem = SizeStr % DWORD_ALIGN;
+          size_t NSizeStr = 0;
+          if (Rem) {
+            NSizeStr = SizeStr + (DWORD_ALIGN - Rem);
+          } else {
+            NSizeStr = SizeStr;
+          }
+          if (S[0]) {
+            char *MyNewStr = new char[NSizeStr]();
+            strcpy(MyNewStr, S);
+            int NumInts = NSizeStr / 4;
+            int CharC = 0;
+            while (NumInts) {
+              int ANum = *(int *)(MyNewStr + CharC);
+              CharC += 4;
+              NumInts--;
+              Value *ANumV = ConstantInt::get(Int32Ty, ANum, false);
               WhatToStore.push_back(ANumV);
             }
+            delete[] MyNewStr;
           } else {
-            uint64_t Size = TD->getTypeAllocSizeInBits(ArgType);
-            assert((Size == 32 || Size == 64) && "unsupported size");
-            Type *DstType = (Size == 32) ? Int32Ty : Int64Ty;
-            Arg = new PtrToIntInst(Arg, DstType, "PrintArgPtr", Brnch);
-            WhatToStore.push_back(Arg);
-          }
-        } else if (isa<FixedVectorType>(ArgType)) {
-          Type *IType = nullptr;
-          uint32_t EleCount = cast<FixedVectorType>(ArgType)->getNumElements();
-          uint32_t EleSize = ArgType->getScalarSizeInBits();
-          uint32_t TotalSize = EleCount * EleSize;
-          if (EleCount == 3) {
-            ShuffleVectorInst *Shuffle =
-                new ShuffleVectorInst(Arg, Arg, ArrayRef<int>{0, 1, 2, 2});
-            Shuffle->insertBefore(Brnch);
-            Arg = Shuffle;
-            ArgType = Arg->getType();
-            TotalSize += EleSize;
+            // Empty string, give a hint to RT it is no NULL
+            Value *ANumV = ConstantInt::get(Int32Ty, 0xFFFFFF00, false);
+            WhatToStore.push_back(ANumV);
           }
-          switch (EleSize) {
-          default:
+        } else {
+          uint64_t Size = TD->getTypeAllocSizeInBits(ArgType);
+          assert((Size == 32 || Size == 64) && "unsupported size");
+          Type *DstType = (Size == 32) ? Int32Ty : Int64Ty;
+          Arg = new PtrToIntInst(Arg, DstType, "PrintArgPtr", Brnch);
+          WhatToStore.push_back(Arg);
+        }
+      } else if (isa<FixedVectorType>(ArgType)) {
+        Type *IType = nullptr;
+        uint32_t EleCount = cast<FixedVectorType>(ArgType)->getNumElements();
+        uint32_t EleSize = ArgType->getScalarSizeInBits();
+        uint32_t TotalSize = EleCount * EleSize;
+        if (EleCount == 3) {
+          ShuffleVectorInst *Shuffle =
+              new ShuffleVectorInst(Arg, Arg, ArrayRef<int>{0, 1, 2, 2});
+          Shuffle->insertBefore(Brnch);
+          Arg = Shuffle;
+          ArgType = Arg->getType();
+          TotalSize += EleSize;
+        }
+        switch (EleSize) {
+        default:
+          EleCount = TotalSize / 64;
+          IType = Type::getInt64Ty(ArgType->getContext());
+          break;
+        case 8:
+          if (EleCount >= 8) {
             EleCount = TotalSize / 64;
             IType = Type::getInt64Ty(ArgType->getContext());
-            break;
-          case 8:
-            if (EleCount >= 8) {
-              EleCount = TotalSize / 64;
-              IType = Type::getInt64Ty(ArgType->getContext());
-            } else if (EleCount >= 3) {
-              EleCount = 1;
-              IType = Type::getInt32Ty(ArgType->getContext());
-            } else {
-              EleCount = 1;
-              IType = Type::getInt16Ty(ArgType->getContext());
-            }
-            break;
-          case 16:
-            if (EleCount >= 3) {
-              EleCount = TotalSize / 64;
-              IType = Type::getInt64Ty(ArgType->getContext());
-            } else {
-              EleCount = 1;
-              IType = Type::getInt32Ty(ArgType->getContext());
-            }
-            break;
+          } else if (EleCount >= 3) {
+            EleCount = 1;
+            IType = Type::getInt32Ty(ArgType->getContext());
+          } else {
+            EleCount = 1;
+            IType = Type::getInt16Ty(ArgType->getContext());
           }
-          if (EleCount > 1) {
-            IType = FixedVectorType::get(IType, EleCount);
+          break;
+        case 16:
+          if (EleCount >= 3) {
+            EleCount = TotalSize / 64;
+            IType = Type::getInt64Ty(ArgType->getContext());
+          } else {
+            EleCount = 1;
+            IType = Type::getInt32Ty(ArgType->getContext());
           }
-          Arg = new BitCastInst(Arg, IType, "PrintArgVect", Brnch);
-          WhatToStore.push_back(Arg);
-        } else {
-          WhatToStore.push_back(Arg);
+          break;
         }
-        for (unsigned I = 0, E = WhatToStore.size(); I != E; ++I) {
-          Value *TheBtCast = WhatToStore[I];
-          unsigned ArgSize =
-              TD->getTypeAllocSizeInBits(TheBtCast->getType()) / 8;
-          SmallVector<Value *, 1> BuffOffset;
-          BuffOffset.push_back(ConstantInt::get(I32Ty, ArgSize));
-
-          Type *ArgPointer = PointerType::get(TheBtCast->getType(), 1);
-          Value *CastedGEP =
-              new BitCastInst(BufferIdx, ArgPointer, "PrintBuffPtrCast", Brnch);
-          StoreInst *StBuff = new StoreInst(TheBtCast, CastedGEP, Brnch);
-          LLVM_DEBUG(dbgs() << "inserting store to printf buffer:\n"
-                            << *StBuff << '\n');
-          (void)StBuff;
-          if (I + 1 == E && ArgCount + 1 == CI->arg_size())
-            break;
-          BufferIdx = GetElementPtrInst::Create(I8Ty, BufferIdx, BuffOffset,
-                                                "PrintBuffNextPtr", Brnch);
-          LLVM_DEBUG(dbgs() << "inserting gep to the printf buffer:\n"
-                            << *BufferIdx << '\n');
+        if (EleCount > 1) {
+          IType = FixedVectorType::get(IType, EleCount);
         }
+        Arg = new BitCastInst(Arg, IType, "PrintArgVect", Brnch);
+        WhatToStore.push_back(Arg);
+      } else {
+        WhatToStore.push_back(Arg);
+      }
+      for (unsigned I = 0, E = WhatToStore.size(); I != E; ++I) {
+        Value *TheBtCast = WhatToStore[I];
+        unsigned ArgSize = TD->getTypeAllocSizeInBits(TheBtCast->getType()) / 8;
+        SmallVector<Value *, 1> BuffOffset;
+        BuffOffset.push_back(ConstantInt::get(I32Ty, ArgSize));
+
+        Type *ArgPointer = PointerType::get(TheBtCast->getType(), 1);
+        Value *CastedGEP =
+            new BitCastInst(BufferIdx, ArgPointer, "PrintBuffPtrCast", Brnch);
+        StoreInst *StBuff = new StoreInst(TheBtCast, CastedGEP, Brnch);
+        LLVM_DEBUG(dbgs() << "inserting store to printf buffer:\n"
+                          << *StBuff << '\n');
+        (void)StBuff;
+        if (I + 1 == E && ArgCount + 1 == CI->arg_size())
+          break;
+        BufferIdx = GetElementPtrInst::Create(I8Ty, BufferIdx, BuffOffset,
+                                              "PrintBuffNextPtr", Brnch);
+        LLVM_DEBUG(dbgs() << "inserting gep to the printf buffer:\n"
+                          << *BufferIdx << '\n');
       }
     }
   }


        


More information about the llvm-commits mailing list