@@ -94,6 +94,210 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
9494
9595 mlir::Value VisitUnaryExprOrTypeTraitExpr (const UnaryExprOrTypeTraitExpr *e);
9696
97+ // Unary Operators.
98+ mlir::Value VisitUnaryPostDec (const UnaryOperator *e) {
99+ LValue lv = cgf.emitLValue (e->getSubExpr ());
100+ return emitScalarPrePostIncDec (e, lv, false , false );
101+ }
102+ mlir::Value VisitUnaryPostInc (const UnaryOperator *e) {
103+ LValue lv = cgf.emitLValue (e->getSubExpr ());
104+ return emitScalarPrePostIncDec (e, lv, true , false );
105+ }
106+ mlir::Value VisitUnaryPreDec (const UnaryOperator *e) {
107+ LValue lv = cgf.emitLValue (e->getSubExpr ());
108+ return emitScalarPrePostIncDec (e, lv, false , true );
109+ }
110+ mlir::Value VisitUnaryPreInc (const UnaryOperator *e) {
111+ LValue lv = cgf.emitLValue (e->getSubExpr ());
112+ return emitScalarPrePostIncDec (e, lv, true , true );
113+ }
114+ mlir::Value emitScalarPrePostIncDec (const UnaryOperator *e, LValue lv,
115+ bool isInc, bool isPre) {
116+ if (cgf.getLangOpts ().OpenMP )
117+ cgf.cgm .errorNYI (e->getSourceRange (), " inc/dec OpenMP" );
118+
119+ QualType type = e->getSubExpr ()->getType ();
120+
121+ mlir::Value value;
122+ mlir::Value input;
123+
124+ if (type->getAs <AtomicType>()) {
125+ cgf.cgm .errorNYI (e->getSourceRange (), " Atomic inc/dec" );
126+ // TODO(cir): This is not correct, but it will produce reasonable code
127+ // until atomic operations are implemented.
128+ value = cgf.emitLoadOfLValue (lv, e->getExprLoc ()).getScalarVal ();
129+ input = value;
130+ } else {
131+ value = cgf.emitLoadOfLValue (lv, e->getExprLoc ()).getScalarVal ();
132+ input = value;
133+ }
134+
135+ // NOTE: When possible, more frequent cases are handled first.
136+
137+ // Special case of integer increment that we have to check first: bool++.
138+ // Due to promotion rules, we get:
139+ // bool++ -> bool = bool + 1
140+ // -> bool = (int)bool + 1
141+ // -> bool = ((int)bool + 1 != 0)
142+ // An interesting aspect of this is that increment is always true.
143+ // Decrement does not have this property.
144+ if (isInc && type->isBooleanType ()) {
145+ value = builder.create <cir::ConstantOp>(cgf.getLoc (e->getExprLoc ()),
146+ cgf.convertType (type),
147+ builder.getCIRBoolAttr (true ));
148+ } else if (type->isIntegerType ()) {
149+ QualType promotedType;
150+ bool canPerformLossyDemotionCheck = false ;
151+ if (cgf.getContext ().isPromotableIntegerType (type)) {
152+ promotedType = cgf.getContext ().getPromotedIntegerType (type);
153+ assert (promotedType != type && " Shouldn't promote to the same type." );
154+ canPerformLossyDemotionCheck = true ;
155+ canPerformLossyDemotionCheck &=
156+ cgf.getContext ().getCanonicalType (type) !=
157+ cgf.getContext ().getCanonicalType (promotedType);
158+ canPerformLossyDemotionCheck &=
159+ type->isIntegerType () && promotedType->isIntegerType ();
160+
161+ // TODO(cir): Currently, we store bitwidths in CIR types only for
162+ // integers. This might also be required for other types.
163+
164+ assert (
165+ (!canPerformLossyDemotionCheck ||
166+ type->isSignedIntegerOrEnumerationType () ||
167+ promotedType->isSignedIntegerOrEnumerationType () ||
168+ mlir::cast<cir::IntType>(cgf.convertType (type)).getWidth () ==
169+ mlir::cast<cir::IntType>(cgf.convertType (type)).getWidth ()) &&
170+ " The following check expects that if we do promotion to different "
171+ " underlying canonical type, at least one of the types (either "
172+ " base or promoted) will be signed, or the bitwidths will match." );
173+ }
174+
175+ assert (!cir::MissingFeatures::sanitizers ());
176+ if (e->canOverflow () && type->isSignedIntegerOrEnumerationType ()) {
177+ value = emitIncDecConsiderOverflowBehavior (e, value, isInc);
178+ } else {
179+ cir::UnaryOpKind kind =
180+ e->isIncrementOp () ? cir::UnaryOpKind::Inc : cir::UnaryOpKind::Dec;
181+ // NOTE(CIR): clang calls CreateAdd but folds this to a unary op
182+ value = emitUnaryOp (e, kind, input);
183+ }
184+ } else if (const PointerType *ptr = type->getAs <PointerType>()) {
185+ cgf.cgm .errorNYI (e->getSourceRange (), " Unary inc/dec pointer" );
186+ return {};
187+ } else if (type->isVectorType ()) {
188+ cgf.cgm .errorNYI (e->getSourceRange (), " Unary inc/dec vector" );
189+ return {};
190+ } else if (type->isRealFloatingType ()) {
191+ assert (!cir::MissingFeatures::CGFPOptionsRAII ());
192+
193+ if (type->isHalfType () &&
194+ !cgf.getContext ().getLangOpts ().NativeHalfType ) {
195+ cgf.cgm .errorNYI (e->getSourceRange (), " Unary inc/dec half" );
196+ return {};
197+ }
198+
199+ if (mlir::isa<cir::SingleType, cir::DoubleType>(value.getType ())) {
200+ // Create the inc/dec operation.
201+ // NOTE(CIR): clang calls CreateAdd but folds this to a unary op
202+ cir::UnaryOpKind kind =
203+ (isInc ? cir::UnaryOpKind::Inc : cir::UnaryOpKind::Dec);
204+ value = emitUnaryOp (e, kind, value);
205+ } else {
206+ cgf.cgm .errorNYI (e->getSourceRange (), " Unary inc/dec other fp type" );
207+ return {};
208+ }
209+ } else if (type->isFixedPointType ()) {
210+ cgf.cgm .errorNYI (e->getSourceRange (), " Unary inc/dec other fixed point" );
211+ return {};
212+ } else {
213+ assert (type->castAs <ObjCObjectPointerType>());
214+ cgf.cgm .errorNYI (e->getSourceRange (), " Unary inc/dec ObjectiveC pointer" );
215+ return {};
216+ }
217+
218+ CIRGenFunction::SourceLocRAIIObject sourceloc{
219+ cgf, cgf.getLoc (e->getSourceRange ())};
220+
221+ // Store the updated result through the lvalue
222+ if (lv.isBitField ()) {
223+ cgf.cgm .errorNYI (e->getSourceRange (), " Unary inc/dec bitfield" );
224+ return {};
225+ } else {
226+ cgf.emitStoreThroughLValue (RValue::get (value), lv);
227+ }
228+
229+ // If this is a postinc, return the value read from memory, otherwise use
230+ // the updated value.
231+ return isPre ? value : input;
232+ }
233+
234+ mlir::Value emitIncDecConsiderOverflowBehavior (const UnaryOperator *e,
235+ mlir::Value inVal,
236+ bool isInc) {
237+ assert (!cir::MissingFeatures::opUnarySignedOverflow ());
238+ cir::UnaryOpKind kind =
239+ e->isIncrementOp () ? cir::UnaryOpKind::Inc : cir::UnaryOpKind::Dec;
240+ switch (cgf.getLangOpts ().getSignedOverflowBehavior ()) {
241+ case LangOptions::SOB_Defined:
242+ return emitUnaryOp (e, kind, inVal);
243+ case LangOptions::SOB_Undefined:
244+ assert (!cir::MissingFeatures::sanitizers ());
245+ return emitUnaryOp (e, kind, inVal);
246+ break ;
247+ case LangOptions::SOB_Trapping:
248+ if (!e->canOverflow ())
249+ return emitUnaryOp (e, kind, inVal);
250+ cgf.cgm .errorNYI (e->getSourceRange (), " inc/def overflow SOB_Trapping" );
251+ return {};
252+ }
253+ llvm_unreachable (" Unexpected signed overflow behavior kind" );
254+ }
255+
256+ mlir::Value VisitUnaryPlus (const UnaryOperator *e,
257+ QualType promotionType = QualType()) {
258+ if (!promotionType.isNull ())
259+ cgf.cgm .errorNYI (e->getSourceRange (), " VisitUnaryPlus: promotionType" );
260+ assert (!cir::MissingFeatures::opUnaryPromotionType ());
261+ mlir::Value result = emitUnaryPlusOrMinus (e, cir::UnaryOpKind::Plus);
262+ return result;
263+ }
264+
265+ mlir::Value VisitUnaryMinus (const UnaryOperator *e,
266+ QualType promotionType = QualType()) {
267+ if (!promotionType.isNull ())
268+ cgf.cgm .errorNYI (e->getSourceRange (), " VisitUnaryMinus: promotionType" );
269+ assert (!cir::MissingFeatures::opUnaryPromotionType ());
270+ mlir::Value result = emitUnaryPlusOrMinus (e, cir::UnaryOpKind::Minus);
271+ return result;
272+ }
273+
274+ mlir::Value emitUnaryPlusOrMinus (const UnaryOperator *e,
275+ cir::UnaryOpKind kind) {
276+ ignoreResultAssign = false ;
277+
278+ assert (!cir::MissingFeatures::opUnaryPromotionType ());
279+ mlir::Value operand = Visit (e->getSubExpr ());
280+
281+ assert (!cir::MissingFeatures::opUnarySignedOverflow ());
282+
283+ // NOTE: LLVM codegen will lower this directly to either a FNeg
284+ // or a Sub instruction. In CIR this will be handled later in LowerToLLVM.
285+ return emitUnaryOp (e, kind, operand);
286+ }
287+
288+ mlir::Value emitUnaryOp (const UnaryOperator *e, cir::UnaryOpKind kind,
289+ mlir::Value input) {
290+ return builder.create <cir::UnaryOp>(
291+ cgf.getLoc (e->getSourceRange ().getBegin ()), input.getType (), kind,
292+ input);
293+ }
294+
295+ mlir::Value VisitUnaryNot (const UnaryOperator *e) {
296+ ignoreResultAssign = false ;
297+ mlir::Value op = Visit (e->getSubExpr ());
298+ return emitUnaryOp (e, cir::UnaryOpKind::Not, op);
299+ }
300+
97301 // / Emit a conversion from the specified type to the specified destination
98302 // / type, both of which are CIR scalar types.
99303 // / TODO: do we need ScalarConversionOpts here? Should be done in another
@@ -188,3 +392,10 @@ mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
188392 loc, builder.getAttr <cir::IntAttr>(
189393 cgf.cgm .UInt64Ty , e->EvaluateKnownConstInt (cgf.getContext ())));
190394}
395+
396+ mlir::Value CIRGenFunction::emitScalarPrePostIncDec (const UnaryOperator *E,
397+ LValue LV, bool isInc,
398+ bool isPre) {
399+ return ScalarExprEmitter (*this , builder)
400+ .emitScalarPrePostIncDec (E, LV, isInc, isPre);
401+ }
0 commit comments