[cfe-commits] r46766 - /cfe/trunk/CodeGen/CGExprConstant.cpp
Anders Carlsson
andersca at mac.com
Tue Feb 5 12:54:21 PST 2008
Author: andersca
Date: Tue Feb 5 14:54:21 2008
New Revision: 46766
URL: http://llvm.org/viewvc/llvm-project?rev=46766&view=rev
Log:
Add some binops.
Modified:
cfe/trunk/CodeGen/CGExprConstant.cpp
Modified: cfe/trunk/CodeGen/CGExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExprConstant.cpp?rev=46766&r1=46765&r2=46766&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/CodeGen/CGExprConstant.cpp Tue Feb 5 14:54:21 2008
@@ -109,7 +109,7 @@
const CGRecordLayout *CGR = CGM.getTypes().getCGRecordLayout(TD);
unsigned NumInitElements = ILE->getNumInits();
unsigned NumElements = SType->getNumElements();
-
+
// Initialising an structure requires us to automatically
// initialise any elements that have not been initialised explicitly
unsigned NumInitableElts = std::min(NumInitElements, NumElements);
@@ -327,7 +327,75 @@
return llvm::ConstantExpr::getOr(LHS, RHS);
}
+ llvm::Constant *VisitBinSub(const BinaryOperator *E) {
+ llvm::Constant *LHS = Visit(E->getLHS());
+ llvm::Constant *RHS = Visit(E->getRHS());
+
+ if (!isa<llvm::PointerType>(RHS->getType())) {
+ // pointer - int
+ if (isa<llvm::PointerType>(LHS->getType())) {
+ llvm::Constant *Idx = llvm::ConstantExpr::getNeg(RHS);
+
+ return llvm::ConstantExpr::getGetElementPtr(LHS, &Idx, 1);
+ }
+
+ // int - int
+ return llvm::ConstantExpr::getSub(LHS, RHS);
+ }
+
+ assert(0 && "Unhandled bin sub case!");
+ return 0;
+ }
+
+ llvm::Constant *VisitBinShl(const BinaryOperator *E) {
+ llvm::Constant *LHS = Visit(E->getLHS());
+ llvm::Constant *RHS = Visit(E->getRHS());
+
+ // LLVM requires the LHS and RHS to be the same type: promote or truncate the
+ // RHS to the same size as the LHS.
+ if (LHS->getType() != RHS->getType())
+ RHS = llvm::ConstantExpr::getIntegerCast(RHS, LHS->getType(), false);
+
+ return llvm::ConstantExpr::getShl(LHS, RHS);
+ }
+
+ llvm::Constant *VisitBinMul(const BinaryOperator *E) {
+ llvm::Constant *LHS = Visit(E->getLHS());
+ llvm::Constant *RHS = Visit(E->getRHS());
+ return llvm::ConstantExpr::getMul(LHS, RHS);
+ }
+
+ llvm::Constant *VisitBinDiv(const BinaryOperator *E) {
+ llvm::Constant *LHS = Visit(E->getLHS());
+ llvm::Constant *RHS = Visit(E->getRHS());
+
+ if (LHS->getType()->isFPOrFPVector())
+ return llvm::ConstantExpr::getFDiv(LHS, RHS);
+ else if (E->getType()->isUnsignedIntegerType())
+ return llvm::ConstantExpr::getUDiv(LHS, RHS);
+ else
+ return llvm::ConstantExpr::getSDiv(LHS, RHS);
+ }
+
+ llvm::Constant *VisitBinAdd(const BinaryOperator *E) {
+ llvm::Constant *LHS = Visit(E->getLHS());
+ llvm::Constant *RHS = Visit(E->getRHS());
+
+ if (!E->getType()->isPointerType())
+ return llvm::ConstantExpr::getAdd(LHS, RHS);
+
+ assert(0 && "Unhandled bin add types!");
+ return 0;
+ }
+
+ llvm::Constant *VisitBinAnd(const BinaryOperator *E) {
+ llvm::Constant *LHS = Visit(E->getLHS());
+ llvm::Constant *RHS = Visit(E->getRHS());
+
+ return llvm::ConstantExpr::getAnd(LHS, RHS);
+ }
+
// Utility methods
const llvm::Type *ConvertType(QualType T) {
return CGM.getTypes().ConvertType(T);
More information about the cfe-commits
mailing list