[cfe-commits] r39564 - in /cfe/cfe/trunk/CodeGen: CGExpr.cpp CodeGenFunction.cpp CodeGenFunction.h

clattner at cs.uiuc.edu clattner at cs.uiuc.edu
Wed Jul 11 09:45:39 PDT 2007


Author: clattner
Date: Wed Jul 11 11:45:39 2007
New Revision: 39564

URL: http://llvm.org/viewvc/llvm-project?rev=39564&view=rev
Log:
implement a first hack at codegen'ing the usual unary conversions.
This allows us to compile:

int func() {
  int A[10];
  if (!A) {

to:

define i32 @func() {
entry:
        %A = alloca [10 x i32]          ; <[10 x i32]*> [#uses=1]
        %arraydecay = getelementptr [10 x i32]* %A, i32 0, i32 0                ; <i32*> [#uses=1]
        %tobool = icmp ne i32* %arraydecay, null                ; <i1> [#uses=1]
        %lnot = xor i1 %tobool, true            ; <i1> [#uses=1]
        br i1 %lnot, label %ifthen, label %ifend

-Chris

Modified:
    cfe/cfe/trunk/CodeGen/CGExpr.cpp
    cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp
    cfe/cfe/trunk/CodeGen/CodeGenFunction.h

Modified: cfe/cfe/trunk/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CGExpr.cpp?rev=39564&r1=39563&r2=39564&view=diff

==============================================================================
--- cfe/cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CGExpr.cpp Wed Jul 11 11:45:39 2007
@@ -163,6 +163,52 @@
 //                          Unary Operator Emission
 //===--------------------------------------------------------------------===//
 
+ExprResult CodeGenFunction::EmitExprWithUsualUnaryConversions(const Expr *E, 
+                                                              QualType &ResTy) {
+  ResTy = E->getType().getCanonicalType();
+  
+  if (isa<FunctionType>(ResTy)) { // C99 6.3.2.1p4
+    // Functions are promoted to their address.
+    ResTy = getContext().getPointerType(ResTy);
+    return ExprResult::get(EmitLValue(E).getAddress());
+  } else if (const ArrayType *ary = dyn_cast<ArrayType>(ResTy)) {
+    // C99 6.3.2.1p3
+    ResTy = getContext().getPointerType(ary->getElementType());
+    
+    // FIXME: For now we assume that all source arrays map to LLVM arrays.  This
+    // will not true when we add support for VLAs.
+    llvm::Value *V = EmitLValue(E).getAddress();  // Bitfields can't be arrays.
+    
+    assert(isa<llvm::PointerType>(V->getType()) &&
+           isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
+                                ->getElementType()) &&
+           "Doesn't support VLAs yet!");
+    llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
+    V = Builder.CreateGEP(V, Idx0, Idx0, "arraydecay");
+    return ExprResult::get(V);
+  } else if (ResTy->isPromotableIntegerType()) { // C99 6.3.1.1p2
+    // FIXME: this probably isn't right, pending clarification from Steve.
+    llvm::Value *Val = EmitExpr(E).getVal();
+    
+    // FIXME: this doesn't handle 'char'!.
+    
+    // If the input is a signed integer, sign extend to the destination.
+    if (ResTy->isSignedIntegerType()) {
+      Val = Builder.CreateSExt(Val, LLVMIntTy, "promote");
+    } else {
+      // This handles unsigned types, including bool.
+      Val = Builder.CreateZExt(Val, LLVMIntTy, "promote");
+    }
+    ResTy = getContext().IntTy;
+    
+    return ExprResult::get(Val);
+  }
+  
+  // Otherwise, this is a float, double, int, struct, etc.
+  return EmitExpr(E);
+}
+
+
 ExprResult CodeGenFunction::EmitUnaryOperator(const UnaryOperator *E) {
   switch (E->getOpcode()) {
   default:
@@ -175,12 +221,11 @@
 
 /// C99 6.5.3.3
 ExprResult CodeGenFunction::EmitUnaryLNot(const UnaryOperator *E) {
-  ExprResult Op = EmitExpr(E->getSubExpr());
-
-  //UsualUnary();
+  QualType ResTy;
+  ExprResult Op = EmitExprWithUsualUnaryConversions(E->getSubExpr(), ResTy);
   
   // Compare to zero.
-  Value *BoolVal = EvaluateScalarValueToBool(Op, E->getSubExpr()->getType());
+  Value *BoolVal = EvaluateScalarValueToBool(Op, ResTy);
   
   // Invert value.
   // TODO: Could dynamically modify easy computations here.  For example, if
@@ -188,8 +233,8 @@
   BoolVal = Builder.CreateNot(BoolVal, "lnot");
   
   // ZExt result to int.
-  const llvm::Type *ResTy = ConvertType(E->getType(), E->getOperatorLoc());
-  return ExprResult::get(Builder.CreateZExt(BoolVal, ResTy, "lnot.ext"));
+  const llvm::Type *ResLTy = ConvertType(E->getType(), E->getOperatorLoc());
+  return ExprResult::get(Builder.CreateZExt(BoolVal, ResLTy, "lnot.ext"));
 }
 
 

Modified: cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp?rev=39564&r1=39563&r2=39564&view=diff

==============================================================================
--- cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp Wed Jul 11 11:45:39 2007
@@ -26,6 +26,10 @@
 CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) 
   : CGM(cgm), Target(CGM.getContext().Target) {}
 
+ASTContext &CodeGenFunction::getContext() const {
+  return CGM.getContext();
+}
+
 
 llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
   BasicBlock *&BB = LabelMap[S];
@@ -131,6 +135,8 @@
 
 
 void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
+  LLVMIntTy = ConvertType(getContext().IntTy, FD->getLocation());
+  
   const llvm::FunctionType *Ty = 
     cast<llvm::FunctionType>(ConvertType(FD->getType(), FD->getLocation()));
   

Modified: cfe/cfe/trunk/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CodeGenFunction.h?rev=39564&r1=39563&r2=39564&view=diff

==============================================================================
--- cfe/cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/cfe/trunk/CodeGen/CodeGenFunction.h Wed Jul 11 11:45:39 2007
@@ -120,6 +120,8 @@
   /// we prefer to insert allocas.
   llvm::Instruction *AllocaInsertPt;
   
+  const llvm::Type *LLVMIntTy;
+  
   /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
   /// decls.
   DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
@@ -129,6 +131,8 @@
 public:
   CodeGenFunction(CodeGenModule &cgm);
   
+  ASTContext &getContext() const;
+
   const llvm::Type *ConvertType(QualType T, SourceLocation Loc);
   
   void GenerateCode(const FunctionDecl *FD);
@@ -180,7 +184,7 @@
   ExprResult EmitExpr(const Expr *E);
   ExprResult EmitIntegerLiteral(const IntegerLiteral *E);
   
-
+  ExprResult EmitExprWithUsualUnaryConversions(const Expr *E, QualType &ResTy);
   void EmitUsualArithmeticConversions(const BinaryOperator *E,
                                       ExprResult &LHS, ExprResult &RHS);
   





More information about the cfe-commits mailing list