]> granicus.if.org Git - clang/commitdiff
simplify the clang codegen by using the new Builder.CreateStructGEP method.
authorChris Lattner <sabre@nondot.org>
Wed, 19 Mar 2008 05:19:41 +0000 (05:19 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 19 Mar 2008 05:19:41 +0000 (05:19 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@48534 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExpr.cpp
lib/CodeGen/CGExprAgg.cpp
lib/CodeGen/CGExprComplex.cpp
lib/CodeGen/CGExprScalar.cpp

index 932a5c5da8b8c00226289c4199138c17edaa3e4b..1642e2d3ee3b08fd750c7dd4b25d439a226ab7d8 100644 (file)
@@ -367,13 +367,9 @@ LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
   case UnaryOperator::Real:
   case UnaryOperator::Imag:
     LValue LV = EmitLValue(E->getSubExpr());
-
-    llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
-    llvm::Constant *Idx  = llvm::ConstantInt::get(llvm::Type::Int32Ty,
-                                        E->getOpcode() == UnaryOperator::Imag);
-    llvm::Value *Ops[] = {Zero, Idx};
-    return LValue::MakeAddr(Builder.CreateGEP(LV.getAddress(), Ops, Ops+2,
-                                              "idx"));
+    unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
+    return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
+                                                    Idx, "idx"));
   }
 }
 
@@ -490,9 +486,11 @@ LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
   llvm::Value *V;
   unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
 
-  if (Field->isBitField()) {
-    const llvm::Type * FieldTy = ConvertType(Field->getType());
-    const llvm::PointerType * BaseTy =
+  if (!Field->isBitField()) {
+    V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
+  } else {
+    const llvm::Type *FieldTy = ConvertType(Field->getType());
+    const llvm::PointerType *BaseTy =
       cast<llvm::PointerType>(BaseValue->getType());
     unsigned AS = BaseTy->getAddressSpace();
     BaseValue = Builder.CreateBitCast(BaseValue,
@@ -501,11 +499,8 @@ LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
     V = Builder.CreateGEP(BaseValue,
                           llvm::ConstantInt::get(llvm::Type::Int32Ty, idx),
                           "tmp");
-  } else {
-    llvm::Value *Idxs[2] = { llvm::Constant::getNullValue(llvm::Type::Int32Ty),
-                             llvm::ConstantInt::get(llvm::Type::Int32Ty, idx) };
-    V = Builder.CreateGEP(BaseValue,Idxs, Idxs + 2, "tmp");
   }
+  
   // Match union field type.
   if (isUnion) {
     const llvm::Type * FieldTy = ConvertType(Field->getType());
@@ -519,13 +514,13 @@ LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
     }
   }
 
-  if (Field->isBitField()) {
-    CodeGenTypes::BitFieldInfo bitFieldInfo =
-      CGM.getTypes().getBitFieldInfo(Field);
-    return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size,
-                                Field->getType()->isSignedIntegerType());
-  } else
+  if (!Field->isBitField())
     return LValue::MakeAddr(V);
+    
+  CodeGenTypes::BitFieldInfo bitFieldInfo =
+    CGM.getTypes().getBitFieldInfo(Field);
+  return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size,
+                              Field->getType()->isSignedIntegerType());
 }
 
 //===--------------------------------------------------------------------===//
index 325ac2109ebc590bca92270a2fee218aafa55382..f57c2ed8816b0ac442e19e33c23fc5663a962bd5 100644 (file)
@@ -268,15 +268,9 @@ void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
   if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
     unsigned NumInitElements = E->getNumInits();
 
-    llvm::Value *Idxs[] = {
-      llvm::Constant::getNullValue(llvm::Type::Int32Ty),
-      NULL
-    };
-    llvm::Value *NextVal = NULL;
     unsigned i;
     for (i = 0; i != NumInitElements; ++i) {
-      Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
-      NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2,".array");
+      llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
       Expr *Init = E->getInit(i);
       if (isa<InitListExpr>(Init))
         CGF.EmitAggExpr(Init, NextVal, VolatileDest);
@@ -289,8 +283,7 @@ void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
     QualType QType = E->getInit(0)->getType();
     const llvm::Type *EType = AType->getElementType();
     for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
-      Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
-      NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2,".array");
+      llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
       if (EType->isFirstClassType())
         Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
       else
index b1de93570d94cfd40198fde8cca421cb1ef84e35..e25ee9563a13e485a2d601b66c944bedc9a7ddec 100644 (file)
@@ -185,15 +185,11 @@ public:
 /// load the real and imaginary pieces, returning them as Real/Imag.
 ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr,
                                                     bool isVolatile) {
-  llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
-  llvm::Constant *One  = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1);
-  
   llvm::SmallString<64> Name(SrcPtr->getNameStart(),
                              SrcPtr->getNameStart()+SrcPtr->getNameLen());
   
   Name += ".realp";
-  llvm::Value *Ops[] = {Zero, Zero};
-  llvm::Value *RealPtr = Builder.CreateGEP(SrcPtr, Ops, Ops+2, Name.c_str());
+  llvm::Value *RealPtr = Builder.CreateStructGEP(SrcPtr, 0, Name.c_str());
 
   Name.pop_back();  // .realp -> .real
   llvm::Value *Real = Builder.CreateLoad(RealPtr, isVolatile, Name.c_str());
@@ -201,8 +197,7 @@ ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr,
   Name.resize(Name.size()-4); // .real -> .imagp
   Name += "imagp";
   
-  Ops[1] = One; // { Ops = { Zero, One }
-  llvm::Value *ImagPtr = Builder.CreateGEP(SrcPtr, Ops, Ops+2, Name.c_str());
+  llvm::Value *ImagPtr = Builder.CreateStructGEP(SrcPtr, 1, Name.c_str());
 
   Name.pop_back();  // .imagp -> .imag
   llvm::Value *Imag = Builder.CreateLoad(ImagPtr, isVolatile, Name.c_str());
@@ -213,14 +208,8 @@ ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr,
 /// specified value pointer.
 void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr,
                                             bool isVolatile) {
-  llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
-  llvm::Constant *One  = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1);
-
-  llvm::Value *Ops[] = {Zero, Zero};
-  llvm::Value *RealPtr = Builder.CreateGEP(Ptr, Ops, Ops+2, "real");
-  
-  Ops[1] = One; // { Ops = { Zero, One }
-  llvm::Value *ImagPtr = Builder.CreateGEP(Ptr, Ops, Ops+2, "imag");
+  llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, "real");
+  llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, "imag");
   
   Builder.CreateStore(Val.first, RealPtr, isVolatile);
   Builder.CreateStore(Val.second, ImagPtr, isVolatile);
index 892712a0d4cab7da709cbb76576ee7129a888dac..2d42f8f812a01322aa664f027573432e02737628 100644 (file)
@@ -524,10 +524,7 @@ Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
            isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
                                 ->getElementType()) &&
            "Doesn't support VLAs yet!");
-    llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
-    
-    llvm::Value *Ops[] = {Idx0, Idx0};
-    V = Builder.CreateGEP(V, Ops, Ops+2, "arraydecay");
+    V = Builder.CreateStructGEP(V, 0, "arraydecay");
     
     // The resultant pointer type can be implicitly casted to other pointer
     // types as well, for example void*.
@@ -597,7 +594,7 @@ Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
   if (isa<llvm::PointerType>(InVal->getType())) {
     // FIXME: This isn't right for VLAs.
     NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
-    NextVal = Builder.CreateGEP(InVal, NextVal);
+    NextVal = Builder.CreateGEP(InVal, NextVal, "ptrincdec");
   } else {
     // Add the inc/dec to the real part.
     if (isa<llvm::IntegerType>(InVal->getType()))