[cfe-commits] r46258 - in /cfe/trunk: CodeGen/CGExpr.cpp CodeGen/CodeGenFunction.h test/CodeGen/bitfield.c
Lauro Ramos Venancio
lauro.venancio at gmail.com
Tue Jan 22 14:36:47 PST 2008
Author: laurov
Date: Tue Jan 22 16:36:45 2008
New Revision: 46258
URL: http://llvm.org/viewvc/llvm-project?rev=46258&view=rev
Log:
Implement bitfield write.
Modified:
cfe/trunk/CodeGen/CGExpr.cpp
cfe/trunk/CodeGen/CodeGenFunction.h
cfe/trunk/test/CodeGen/bitfield.c
Modified: cfe/trunk/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExpr.cpp?rev=46258&r1=46257&r2=46258&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/CodeGen/CGExpr.cpp Tue Jan 22 16:36:45 2008
@@ -237,8 +237,11 @@
// If this is an update of elements of a vector, insert them as appropriate.
if (Dst.isOCUVectorElt())
return EmitStoreThroughOCUComponentLValue(Src, Dst, Ty);
-
- assert(0 && "FIXME: Don't support store to bitfield yet");
+
+ if (Dst.isBitfield())
+ return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
+
+ assert(0 && "Unknown bitfield type");
}
llvm::Value *DstAddr = Dst.getAddress();
@@ -256,6 +259,38 @@
Builder.CreateStore(Src.getScalarVal(), DstAddr);
}
+void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
+ QualType Ty) {
+ unsigned short StartBit = Dst.getBitfieldStartBit();
+ unsigned short BitfieldSize = Dst.getBitfieldSize();
+ llvm::Value *Ptr = Dst.getBitfieldAddr();
+ const llvm::Type *EltTy =
+ cast<llvm::PointerType>(Ptr->getType())->getElementType();
+ unsigned EltTySize = EltTy->getPrimitiveSizeInBits();
+
+ llvm::Value *NewVal = Src.getScalarVal();
+ llvm::Value *OldVal = Builder.CreateLoad(Ptr, "tmp");
+
+ llvm::Value *ShAmt = llvm::ConstantInt::get(EltTy, StartBit);
+ NewVal = Builder.CreateShl(NewVal, ShAmt, "tmp");
+
+ llvm::Constant *Mask = llvm::ConstantInt::get(
+ llvm::APInt::getBitsSet(EltTySize, StartBit,
+ StartBit + BitfieldSize - 1));
+
+ // Mask out any bits that shouldn't be set in the result.
+ NewVal = Builder.CreateAnd(NewVal, Mask, "tmp");
+
+ // Next, mask out the bits this bit-field should include from the old value.
+ Mask = llvm::ConstantExpr::getNot(Mask);
+ OldVal = Builder.CreateAnd(OldVal, Mask, "tmp");
+
+ // Finally, merge the two together and store it.
+ NewVal = Builder.CreateOr(OldVal, NewVal, "tmp");
+
+ Builder.CreateStore(NewVal, Ptr);
+}
+
void CodeGenFunction::EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst,
QualType Ty) {
// This access turns into a read/modify/write of the vector. Load the input
Modified: cfe/trunk/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenFunction.h?rev=46258&r1=46257&r2=46258&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/CodeGen/CodeGenFunction.h Tue Jan 22 16:36:45 2008
@@ -401,6 +401,7 @@
/// is 'Ty'.
void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
void EmitStoreThroughOCUComponentLValue(RValue Src, LValue Dst, QualType Ty);
+ void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty);
// Note: only availabe for agg return types
LValue EmitCallExprLValue(const CallExpr *E);
Modified: cfe/trunk/test/CodeGen/bitfield.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/bitfield.c?rev=46258&r1=46257&r2=46258&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/bitfield.c (original)
+++ cfe/trunk/test/CodeGen/bitfield.c Tue Jan 22 16:36:45 2008
@@ -3,6 +3,8 @@
// RUN: grep "ashr i32 %tmp1, 19" %t1 &&
// RUN: grep "shl i16 %tmp4, 1" %t1 &&
// RUN: grep "lshr i16 %tmp5, 9" %t1
+// RUN: grep "and i32 %tmp, -8192" %t1
+// RUN: grep "and i16 %tmp5, -32513" %t1
// Test bitfield access
@@ -11,3 +13,9 @@
int f() {
return stb1.a + stb1.b + stb1.c;
}
+
+void g() {
+ stb1.a = -40;
+ stb1.b = 10;
+ stb1.c = 15;
+}
More information about the cfe-commits
mailing list