[cfe-commits] r44744 - /cfe/trunk/CodeGen/CGBuiltin.cpp

Anders Carlsson andersca at mac.com
Sun Dec 9 13:20:04 PST 2007


Author: andersca
Date: Sun Dec  9 15:20:04 2007
New Revision: 44744

URL: http://llvm.org/viewvc/llvm-project?rev=44744&view=rev
Log:
Generate code for target specific intrinsics.

Modified:
    cfe/trunk/CodeGen/CGBuiltin.cpp

Modified: cfe/trunk/CodeGen/CGBuiltin.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGBuiltin.cpp?rev=44744&r1=44743&r2=44744&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGBuiltin.cpp (original)
+++ cfe/trunk/CodeGen/CGBuiltin.cpp Sun Dec  9 15:20:04 2007
@@ -13,6 +13,7 @@
 
 #include "CodeGenFunction.h"
 #include "CodeGenModule.h"
+#include "clang/Basic/TargetInfo.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Builtins.h"
 #include "clang/AST/Expr.h"
@@ -23,11 +24,61 @@
 using namespace clang;
 using namespace CodeGen;
 
+using namespace llvm;
+
 RValue CodeGenFunction::EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E) {
   switch (BuiltinID) {
-  default:
+  default: {
     if (getContext().BuiltinInfo.isLibFunction(BuiltinID))
       return EmitCallExpr(CGM.getBuiltinLibFunction(BuiltinID), E);
+  
+    // See if we have a target specific intrinsic.
+    llvm::Intrinsic::ID IntrinsicID;
+    const char *TargetPrefix = Target.getTargetPrefix();
+    const char *BuiltinName = getContext().BuiltinInfo.GetName(BuiltinID);
+#define GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
+#include "llvm/Intrinsics.gen"
+#undef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN
+    
+    if (IntrinsicID != Intrinsic::not_intrinsic) {
+      llvm::SmallVector<llvm::Value*, 16> Args;
+      
+      llvm::Function *F = llvm::Intrinsic::getDeclaration(&CGM.getModule(), 
+                                                          IntrinsicID);
+      
+      const llvm::FunctionType *FTy = F->getFunctionType();
+      
+      for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
+        llvm::Value *ArgValue = EmitScalarExpr(E->getArg(i));
+  
+        // If the intrinsic arg type is different from the builtin arg type
+        // we need to do a bit cast.
+        const llvm::Type *PTy = FTy->getParamType(i);
+        if (PTy != ArgValue->getType()) {
+          assert(PTy->canLosslesslyBitCastTo(FTy->getParamType(i)) &&
+                 "Must be able to losslessly bit cast to param");
+          ArgValue = Builder.CreateBitCast(ArgValue, PTy);
+        }
+
+        Args.push_back(ArgValue);
+      }
+            
+      llvm::Value *V = Builder.CreateCall(F, &Args[0], &Args[0] + Args.size());
+
+      QualType BuiltinRetType = E->getType();
+      
+      const llvm::Type *RetTy = BuiltinRetType->isVoidType() ? 
+        llvm::Type::VoidTy : ConvertType(BuiltinRetType);
+
+      if (RetTy != V->getType()) {
+        assert(V->getType()->canLosslesslyBitCastTo(RetTy) &&
+               "Must be able to losslessly bit cast result type");
+        
+        V = Builder.CreateBitCast(V, RetTy);
+      }
+      
+      return RValue::get(V);
+    }
     
     WarnUnsupported(E, "builtin function");
 
@@ -35,7 +86,7 @@
     if (hasAggregateLLVMType(E->getType()))
       return RValue::getAggregate(CreateTempAlloca(ConvertType(E->getType())));
     return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
-    
+  }    
   case Builtin::BI__builtin___CFStringMakeConstantString: {
     const Expr *Arg = E->getArg(0);
     





More information about the cfe-commits mailing list